package com.tuanzi.loan.business.service.banana.collaborate.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.collaborate.NewCollaborateChannelRatio;
import com.tuanzi.loan.business.entity.collaborate.NewCollaborateChannelUser;
import com.tuanzi.loan.business.enums.CollaborateColShowEnum;
import com.tuanzi.loan.business.repository.banana.collaborate.custom.RegistryRepositoryCustom;
import com.tuanzi.loan.business.repository.collaborate.CollaborateChannelRatioRepository;
import com.tuanzi.loan.business.repository.collaborate.CollaborateChannelUserRepository;
import com.tuanzi.loan.business.service.banana.collaborate.BananaRegistryService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.Collaborate.CollaborateStatListRequest;
import com.tuanzi.loan.vo.response.collaborate.CollaborateStatListResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 描述:
 *
 * @author YJM
 * @create 2018-11-15 10:35 AM
 */

@Debug
@Slf4j
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class BananaRegistryServiceImpl implements BananaRegistryService {

	@Autowired
	private RegistryRepositoryCustom registryRepository;
	@Autowired
	private CollaborateChannelUserRepository collaborateChannelUserRepository;
	@Autowired
	private CollaborateChannelRatioRepository collaborateChannelRatioRepository;

	@Override
	public Page<CollaborateStatListResponse> list(PageSearcher<CollaborateStatListRequest> pageSearcher) {
		String account = CurrentUser.get().getAccount();
		if(StringUtils.isBlank(account)){
			return new PageImpl<>(Lists.newArrayList());
		}
		List<NewCollaborateChannelUser> byUserName = collaborateChannelUserRepository.findByUserName(account);
		byUserName = byUserName.stream().filter(x->x.getProductId().equals(3)).collect(Collectors.toList());
		if(Objects.isNull(byUserName) || byUserName.size() <= 0){
			return new PageImpl<>(Lists.newArrayList());
		}

		List<Integer> ChannelList = byUserName.stream().map(NewCollaborateChannelUser::getChannelId).collect(Collectors.toList());
		CollaborateStatListRequest searchCondition = pageSearcher.getSearchCondition();
		searchCondition.setChannelList(ChannelList);

		List<NewCollaborateChannelRatio> channelRatios = collaborateChannelRatioRepository.findByChannelIdIn(ChannelList);

		Page<CollaborateStatListResponse> collaborateStatListResponses = registryRepository.list(searchCondition, pageSearcher.getPageable());

		setSomeCollaborateDate(channelRatios, collaborateStatListResponses);

		return collaborateStatListResponses;
	}

	private void setSomeCollaborateDate(List<NewCollaborateChannelRatio> ratios, Page<CollaborateStatListResponse> responses){

		long todayTimestamp = Timestamp.valueOf(DateUtils.getCurrentDate() +" 00:00:00").getTime();

		responses.forEach(resp -> {
			for (NewCollaborateChannelRatio channelRatio : ratios) {
				if(resp.getChannelId().equals(channelRatio.getChannelId().toString())){

					//当天的数据要乘以系数
					if(resp.getDate().getTime() == todayTimestamp){
						Integer ratio = channelRatio.getRatio();
						Integer externalRegisterCount = resp.getExternalRegisterCount();
						Integer externalNewRegisterCount = resp.getExternalNewRegisterCount();
						Integer appActivateCount = resp.getAppActivateCount();
						Integer appActivateCountPhone = resp.getAppActivateCountPhone();

						resp.setExternalRegisterCount(Math.round((float)externalRegisterCount*ratio/100));
						resp.setExternalNewRegisterCount(Math.round((float)externalNewRegisterCount*ratio/100));
						resp.setAppActivateCount(Math.round((float)appActivateCount*ratio/100));
						resp.setAppActivateCountPhone(Math.round((float)appActivateCountPhone*ratio/100));
					}

					Integer showType = channelRatio.getShowType();
					CollaborateColShowEnum anEnum = CollaborateColShowEnum.getEnum(showType);
					if(Objects.isNull(anEnum)){
						continue;
					}

					switch (anEnum){
						case H5_REGISTER:
							resp.setExternalNewRegisterCount(null);
							resp.setAppActivateCountPhone(null);
							resp.setAppActivateCount(null);
							break;
						case H5_NEW_REGISTER:
							resp.setExternalRegisterCount(null);
							resp.setAppActivateCount(null);
							resp.setAppActivateCountPhone(null);
							break;
						case APP_COUNT:
							//ios平台的app注册数都使用按手机号表示
							if("ios".equalsIgnoreCase(resp.getPlatform())){
								resp.setAppActivateCount(resp.getAppActivateCountPhone());
							}
							resp.setExternalRegisterCount(null);
							resp.setExternalNewRegisterCount(null);
							resp.setAppActivateCountPhone(null);
							break;
						case APP_COUNT_PHONE:
							resp.setExternalRegisterCount(null);
							resp.setExternalNewRegisterCount(null);
							resp.setAppActivateCount(null);
							break;
						case ALL:
							//ios平台的app注册数都使用按手机号表示
							if("ios".equalsIgnoreCase(resp.getPlatform())){
								resp.setAppActivateCount(resp.getAppActivateCountPhone());
							}
							break;
						default:
							break;
					}
				}
			}
		});
	}

}
